<?php

namespace Biz\tools;

use Doraemon\pockets\config\IniConfig;


class  Mail
{
    const _CONFIG_FILE_ = 'email';

    const _CONFIG_SELECT_ = 'gmail_noreply';


    protected static $configArray = array();

    public static function init()
    {
        $config = IniConfig::getConfigSelect(self::_CONFIG_FILE_, self::_CONFIG_SELECT_);
        self::$configArray = $config;
    }


    public static function send($email, $title, $body, $filePath = null, $fileName='', $cc = [])
    {
        self::init();
        $config = self::$configArray;
        if (is_array($email)) {
            $email = $email;
        } else {
            $email = [$email];
        }
        $transport = (new \Swift_SmtpTransport($config['email_host'], $config['email_port']));
        $mailer = new \Swift_Mailer($transport);
        $message = (new \Swift_Message($title))
            ->setFrom([$config['email_from'] => '好好住'])
            ->setTo($email)
            ->setContentType("text/html")
            ->setBody($body);
        if ($filePath) {
            $attachment = \Swift_Attachment::fromPath($filePath);
            if ($fileName) {
                $attachment->setFilename($fileName);
            }
            $message->attach($attachment);
        }

        if ($cc) {
            foreach ($cc as $v) {
                $message->addCc($v['address'], $v['name']);
            }
        }

        $result = $mailer->send($message);
        return $result;
    }
}